package homework;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/doReg3.action")
public class Servlet03 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        PrintWriter out=response.getWriter();
        String username=request.getParameter("username");
        String password=request.getParameter("psd");
        String day=request.getParameter("day");
        String week=request.getParameter("week");
        String month=request.getParameter("month");
        String no=request.getParameter("no");


        HttpSession session=request.getSession();

        if(username.equalsIgnoreCase("admin") && password.equals("123")){
            session.setAttribute("user",username);
            response.sendRedirect("home.jsp");
            Boolean isAuto1=Boolean.valueOf(day);
            Boolean isAuto2=Boolean.valueOf(week);
            Boolean isAuto3=Boolean.valueOf(month);
            Boolean isAuto4=Boolean.valueOf(no);

            String userInfo=username+"|"+password;
                Cookie userCookie=new Cookie("userInfo",userInfo);
                userCookie.setHttpOnly(true);
                if(isAuto1==true){
                    userCookie.setMaxAge(24 * 3600);
                }else if (isAuto2==true){
                    userCookie.setMaxAge(24 * 3600*7);
                }else if(isAuto3==true){
                    userCookie.setMaxAge(24 * 3600*30);
                }else{
                    userCookie.setMaxAge(0);
                }
                response.addCookie(userCookie);
        }else{
            request.setAttribute("mess","账号或者密码输入有误");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
